//
//  NSString+WH3DES.m
//  BinFengExpressOwner
//
//  Created by 邓伟浩 on 2018/10/24.
//  Copyright © 2018年 BinFeng. All rights reserved.
//

#import "NSString+WH3DES.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (WH3DES)

// 加密
+ (NSString *)encryptUseDES3:(NSString *)plainText key:(NSString *)key {
    NSString *ciphertext = nil;
    const char *textBytes = [plainText UTF8String];
    size_t dataLength = [plainText length];
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    
    bufferPtrSize = (dataLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithm3DES,
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          [key UTF8String], kCCKeySize3DES,
                                          NULL,
                                          textBytes, dataLength,
                                          (void *)bufferPtr, bufferPtrSize,
                                          &movedBytes);
    if (cryptStatus == kCCSuccess) {
        
        ciphertext = [self parseByte2HexString:bufferPtr length:(int)movedBytes];;
        
        
    }
    ciphertext = [ciphertext uppercaseString]; //字符变大写
    
    return ciphertext ;
}

// 二进制转化十六进制
+ (NSString *)parseByte2HexString:(Byte *)bytes length:(int)len {
    NSString *hexStr = @"";
    if(bytes) {
        for (int i = 0; i < len; i++) {
            NSString *newHexStr = [NSString stringWithFormat:@"%x", bytes[i]&0xff]; //16进制数
            if ([newHexStr length] == 1) {
                hexStr = [NSString stringWithFormat:@"%@0%@", hexStr, newHexStr];
            } else  {
                hexStr = [NSString stringWithFormat:@"%@%@", hexStr, newHexStr];
            }
//            NSLog(@"%@", hexStr);
        }
    }
    return hexStr;
}

// 解密
+ (NSString *)decryptUseDES3:(NSString *)cipherText key:(NSString *)key {
    NSData* cipherData = [self convertHexStrToData:[cipherText lowercaseString]];
//    NSLog(@"++++++++///%@", cipherData);
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithm3DES,
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySize3DES,
                                          NULL,
                                          [cipherData bytes],
                                          [cipherData length],
                                          buffer,
                                          1024,
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return plainText;
    
}

// 十六进制转换二进制
+ (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
//    NSLog(@"hexdata: %@", hexData);
    return hexData;
}

@end
